/**
 * HanLPResourceIOAdapter.java
 *
 * @author: ZhuJiahui
 * @date: 2019/1/7 22:36
 */
package com.zhujiahui.nlp.core.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hankcs.hanlp.corpus.io.IIOAdapter;

/**
 * @description: HanLP文件输入输出适配器重写类
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class HanLPResourceIOAdapter implements IIOAdapter {

	private static final Logger LOG = LoggerFactory
			.getLogger(HanLPResourceIOAdapter.class);

	/**
	 * 默认构造方法
	 */
	public HanLPResourceIOAdapter() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hankcs.hanlp.corpus.io.IIOAdapter#create(java.lang.String)
	 */
	@Override
	public OutputStream create(String arg0) throws IOException {
		OutputStream outputStream = null;
		String path = HanLPResourceIOAdapter.class.getClassLoader()
				.getResource("data/hanlp/").getPath();
		String fullPath = path + arg0;
		LOG.info("HanLP模型输出文件名：" + fullPath);

		try {
			outputStream = new FileOutputStream(fullPath);
		} catch (Exception e) {
			LOG.error("无法创建HanLP OutputStream。");
			LOG.error(e.toString());
		}
		return outputStream;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hankcs.hanlp.corpus.io.IIOAdapter#open(java.lang.String)
	 */
	@Override
	public InputStream open(String arg0) throws IOException {
		InputStream inputStream = null;
		String path = HanLPResourceIOAdapter.class.getClassLoader()
				.getResource("data/hanlp/").getPath();
		String fullPath = path + arg0;
		LOG.info("HanLP模型输入文件名：" + fullPath);
		try {
			inputStream = new FileInputStream(fullPath);
		} catch (Exception e) {
			LOG.error("无法创建HanLP InputStream。");
			LOG.error(e.toString());
		}

		return inputStream;
	}

}
